package com.vanke.devops.api.controller.endpoints;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanke.core.bean.ApiBaseResponse;
import com.vanke.core.bean.PagingResponse;
import com.vanke.devops.api.base.AbstractController;
import com.vanke.devops.api.controller.endpoints.form.EndpointsForm;
import com.vanke.devops.dal.model.Endpoints;
import com.vanke.devops.domain.domain.endpoints.IEndpointsService;
import com.vanke.devops.domain.domain.endpoints.dto.EndpointsDTO;
import com.vanke.devops.domain.manager.aliyun.bean.Node;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 节点表  前端控制器
 * </p>
 *
 * @author 陈景阳
 * @since 2018-03-12
 */
@Api(description = "主机")
@RestController
@RequestMapping("/endpoints")
public class EndpointsController extends AbstractController<IEndpointsService> {

    @ApiOperation("分页列表")
    @GetMapping(value = "/page/{pageSize}/{pageNo}")
    public PagingResponse list(@PathVariable("pageSize") Integer pageSize, @PathVariable("pageNo") Integer pageNo)  {
        Page<EndpointsDTO> page = service.page(pageSize, pageNo);
        return setResponse(page);
    }

    @ApiOperation("主机信息")
    @GetMapping(value = "/{id}")
    public ApiBaseResponse getInfo(@PathVariable("id") Integer id)  {
        EndpointsDTO dto = service.getInfo(id);
        return setResponse(dto);
    }

    @ApiOperation("更新")
    @PutMapping(value = "/{id}")
    public ApiBaseResponse update(@Valid @RequestBody EndpointsForm form, @PathVariable("id") Integer id)  {
        Endpoints entity = this.getEndpoints(form);
        entity.setId(id);
        EndpointsDTO dto =  service.modify(entity);
        return setResponse(dto);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}")
    public ApiBaseResponse delete(@PathVariable("id") Integer id)  {
         service.delete(id);
        return setResponse();
    }

    @ApiOperation("新增")
    @PostMapping
    public ApiBaseResponse add(@Valid @RequestBody EndpointsForm form)  {
        Endpoints entity = this.getEndpoints(form);
        EndpointsDTO dto =  service.create(entity);
        return setResponse(dto);
    }

    @ApiOperation("负载信息")
    @GetMapping("/node/{id}")
    public ApiBaseResponse getNodeInfo(@PathVariable("id") Integer id){
        EndpointsDTO endpoint = service.getNodeInfo(id);
        return setResponse(endpoint);
    }

    private Endpoints getEndpoints(EndpointsForm form){
        Endpoints entity = new Endpoints();
        entity.setName(form.getName());
        entity.setIp(form.getIp());
        entity.setInstanceId(form.getInstanceId());
        entity.setGroup(form.getGroup());
        entity.setHostname(form.getHostname());
        entity.setUsername(form.getUsername());
        entity.setPassword(form.getPassword());
        entity.setCpu(form.getCpu());
        entity.setMemory(form.getMemory());
        entity.setDisk(form.getDisk());
        entity.setTelegrafHostname(form.getTelegrafHostname());
        return entity;
    }
}